package com.example.finaltask.Fragment;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.finaltask.Adapter.ListAdapter;
import com.example.finaltask.IndexActivity;
import com.example.finaltask.LoginActivity;
import com.example.finaltask.PostActivity;
import com.example.finaltask.R;
import com.example.finaltask.modle.Poster;
import com.example.finaltask.modle.User;
import com.example.finaltask.utils.DBHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PostFragment extends Fragment {
    private DBHelper db;
    private User user;
    private Context mContext;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private ListView listView;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.mContext = getActivity();
        Bundle bundle = getArguments();
        assert bundle != null;
        user = (User) bundle.getSerializable("user");
    }
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        final View root = inflater.inflate(R.layout.fragment_post, container, false);
        ImageView add = root.findViewById(R.id.add_post);
        listView = root.findViewById(R.id.activity_list);
        mSwipeRefreshLayout = root.findViewById(R.id.refresh);
        List<Poster> posters =getData();
        listView.setAdapter(new ListAdapter(getActivity(), posters,user));
        //去掉框线
        listView.setDivider(null);
        //监听器
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(user!=null){
                    Intent intent = new Intent();
                    intent.setClass(root.getContext(), PostActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("user", (Serializable) user);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext, "请先登录", Toast.LENGTH_LONG).show();
                }
            }
        });
        //下拉刷新
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                List<Poster> posters =getData();
                listView.setAdapter(new ListAdapter(getActivity(), posters,user));
                Toast.makeText(mContext, "刷新成功", Toast.LENGTH_LONG).show();
                mSwipeRefreshLayout.setRefreshing(false);//刷新完成
            }
        });
        return root;
    }
    private List<Poster> getData(){
        List<Poster> list= new ArrayList<Poster>();
        DBHelper dbhelper = new DBHelper( mContext, "murmur.db", null, 7);
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        Cursor cursor =	db.rawQuery("select * from poster",null);
        while(cursor.moveToNext()) {;
            Poster post= new Poster();
            post.setId(cursor.getInt(cursor.getColumnIndex("id")));
            post.setContent(cursor.getString(cursor.getColumnIndex("content")));
            post.setCreateTime(cursor.getString(cursor.getColumnIndex("createtime")));
            //post.setPreviewImageUrl(cursor.getString(cursor.getColumnIndex("previewimageurl")));
            post.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            post.setUserId(cursor.getInt(cursor.getColumnIndex("userid")));
            list.add(post);
        }
        return list;
    }
}